package com.zh.note.huawei.onehundred.最小的调整次数;

import java.util.LinkedList;

/**
 * @Author: zhanghuan
 * @date: 2023/8/3 18:36
 * @description: create it
 */
public class Main {
    /**
     * 总结：用什么数据结构存储，方便排序
     *
     * @param args
     */
    public static void main(String[] args) {
        // Scanner sc = new Scanner(System.in);
        // int n = sc.nextInt();
        // String[] commands = new String[2 * n];
        // for (int i = 0; i < commands.length; i++) {
        //     commands[i] = sc.nextLine();
        // }

        String[] commands = {"head add 1", "remove", "tail add 2", "head add 3", "remove", "remove"};
        int count = 0;
        int input = 0;
        int output = 0;

        LinkedList<Integer> queue = new LinkedList<>();
        for (String command : commands) {
            char ch = command.charAt(0);
            switch (ch) {
                case 'h':
                    queue.addFirst(++input);
                    break;
                case 't':
                    queue.addLast(++input);
                    break;
                default: // 移除
                    if (!queue.getFirst().equals(++output)) { //不是output顺序，需要调整
                        count++;
                        queue.sort(Integer::compareTo);
                    }
                    queue.removeFirst();
                    break;


            }
        }
        System.out.println(count);

    }
}
